﻿Imports DataSet_apaTableAdapters
Imports DataSet_apa
Imports System.Data
Imports System.Data.SqlClient
Imports System.Int32
Imports utileria
Partial Class Reportes_ReporteGral
    Inherits System.Web.UI.Page
    Dim cadena As String
    Dim CampoActivar As String = ""
    Dim sdsReporteCampos As New SqlDataSource
    Dim sSQL, campos As String
    Dim sdsAnimales As New SqlDataSource
    Public Event PageIndexChanging As GridViewPageEventHandler
    Dim LosCampos As String
    Dim CamposReg As New ReporteCamposTableAdapter
    Dim CampoBusqueda As String
    Dim idTable As New filtroDataTable
    Dim util As New utileria
    Dim idAdapt As New filtroTableAdapter
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim salida As Integer
        Dim RegTabla As String
        Dim RegAni, RegTCab, RegTPie, RegLCab, RegLPie, RegEmp, RegSan As Boolean
        ID = Session("filtro")
        If ID = Nothing Then
            Dim sUrl As String = Request.Url.Authority()
            Dim codigo As String = ""
            sUrl = "http://" + sUrl
            codigo += sUrl & "/Santafe/Reportes/MsgBox.aspx?modulo=10"
            Response.Redirect(codigo)
        End If
        idTable = idAdapt.GetFiltroById(ID) ' -- Cargar el encabezado del filtro
        LbFiltro.Text = idTable(0).nombreFiltro
        'Dim ConexionBD As New SqlConnection()
        'ConexionBD.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
        'Dim OrdenSQL As SqlCommand = Nothing
        'Dim Lector As SqlDataReader = Nothing
        'Try
        '    Dim Consulta As String = "Select Tabla from filtroDetalle where idFiltro= " & "'" & ID & "'"
        '    OrdenSQL = New SqlCommand(Consulta, ConexionBD)
        '    ConexionBD.Open()
        '    Lector = OrdenSQL.ExecuteReader()
        '    While salida = 0
        '        If Lector.Read() Then
        '            RegTabla = Lector("Tabla")
        '            If RegTabla = "Animal" Then
        '                RegAni = True
        '            ElseIf RegTabla = "TientaCaballo" Then
        '                RegTCab = True
        '            ElseIf RegTabla = "TientaLidia" Then
        '                RegTPie = True
        '            ElseIf RegTabla = "LidiaCaballo" Then
        '                RegLCab = True
        '            ElseIf RegTabla = "LidiaPie" Then
        '                RegLPie = True
        '            ElseIf RegTabla = "Empadre" Then
        '                RegEmp = True
        '            ElseIf RegTabla = "Sanidad" Then
        '                RegSan = True
        '            End If
        '        Else
        '            salida = 1
        '        End If
        '    End While
        '    ConexionBD.Close()
        'Catch ex As Exception
        'End Try
        'If RegAni = True Then
        '    ListBox1.Enabled = True
        'Else
        '    ListBox1.Enabled = False
        '    ListBox1.BackColor = Drawing.Color.LightGray
        '    BtnTodos1.Enabled = False
        '    BtnNinguno1.Enabled = False
        'End If
        'If RegTCab = True Then
        '    ListBox2.Enabled = True
        'Else
        '    ListBox2.Enabled = False
        '    ListBox2.BackColor = Drawing.Color.LightGray
        '    BtnTodos2.Enabled = False
        '    BtnNinguno2.Enabled = False
        'End If
        'If RegTPie = True Then
        '    ListBox3.Enabled = True
        'Else
        '    ListBox3.Enabled = False
        '    ListBox3.BackColor = Drawing.Color.LightGray
        '    BtnTodos3.Enabled = False
        '    BtnNinguno3.Enabled = False
        'End If
        'If RegLCab = True Then
        '    ListBox4.Enabled = True
        'Else
        '    ListBox4.Enabled = False
        '    ListBox4.BackColor = Drawing.Color.LightGray
        '    BtnTodos4.Enabled = False
        '    BtnNinguno4.Enabled = False
        'End If
        'If RegLPie = True Then
        '    ListBox5.Enabled = True
        'Else
        '    ListBox5.Enabled = False
        '    ListBox5.BackColor = Drawing.Color.LightGray
        '    BtnTodos5.Enabled = False
        '    BtnNinguno5.Enabled = False
        'End If
        'If RegEmp = True Then
        '    ListBox6.Enabled = True
        'Else
        '    ListBox6.Enabled = False
        '    ListBox6.BackColor = Drawing.Color.LightGray
        '    BtnTodos6.Enabled = False
        '    BtnNinguno6.Enabled = False
        'End If
        'If RegSan = True Then
        '    ListBox7.Enabled = True
        'Else
        '    ListBox7.Enabled = False
        '    ListBox7.BackColor = Drawing.Color.LightGray
        '    BtnTodos7.Enabled = False
        '    BtnNinguno7.Enabled = False
        'End If
    End Sub
    Sub cadenaLB()
        cadena = ""
        For Each Item In ListBox1.Items
            If Item.selected Then
                cadena = cadena + Item.ToString + ","
            End If
        Next
        Dim longitud_cadena As Integer
        longitud_cadena = cadena.Length
        cadena = cadena.Remove(longitud_cadena - 1, 1)
    End Sub
    Sub LisBoxDesactiva()
        For Each Item In ListBox1.Items
            If Item.selected Then
                Item.selected = False
            End If
        Next
    End Sub
    Sub LeeDDL()
        Dim CampoReg As String
        Dim ConexionBD As New SqlConnection()
        ConexionBD.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
        ' "Server=gutsasybase2.gutsa.com.mx;Database=ganaderia;User ID=Stafe;Password=gutsa"
        Dim OrdenSQL As SqlCommand = Nothing
        Dim Lector As SqlDataReader = Nothing
        Try
            Dim Consulta As String = "Select NumRepId,NombreRep,Campos from ReporteCampos where NumRepId=" & CampoBusqueda
            OrdenSQL = New SqlCommand(Consulta, ConexionBD)
            ConexionBD.Open()
            Lector = OrdenSQL.ExecuteReader()
            If Lector.Read() Then
                CampoReg = Lector("Campos")
            End If
        Catch ex As Exception
        End Try
        LisBoxDesactiva()
        LosCampos = CampoReg.Trim()
        Dim LongCampos As Integer = CInt(LosCampos.Length)
        Dim Dig As String
        Dim i As Integer = 0
        For i = 0 To LongCampos - 1
            Dig = LosCampos(i)
            If Dig = "," Then
                CambiaSeleccion()
                CampoActivar = ""
            Else
                CampoActivar = CampoActivar & Dig
            End If
        Next
        CambiaSeleccion()
        CampoActivar = ""
    End Sub
    Sub CambiaSeleccion()
        For Each Item In ListBox1.Items
            If Item.ToString = CampoActivar Then
                Item.selected = True
            End If
        Next
    End Sub
    Public Sub ListBoxActiva(ByRef Control As ListBox)
        For Each Item In Control.Items
            If Item.Selected = False Then
                Item.Selected = True
            End If
        Next
    End Sub
    Public Sub ListBoxDesactiva(ByRef Control As ListBox)
        For Each Item In Control.Items
            If Item.Selected = True Then
                Item.Selected = False
            End If
        Next
    End Sub
    Sub LeeCampos()
        ' Forma la parte de la cadena de la tabla animal
        For Each Item In ListBox1.Items
            Dim li As New ListItem
            li = Item
            If li.Selected Then
                'cadena = cadena + "animal." + li.Value + ","
                cadena = cadena + li.Value + ","
            End If
        Next
        ' Forma la parte de la cadena de la tabla TientaCaballo
        For Each Item In ListBox2.Items
            Dim li As New ListItem
            li = Item
            If li.Selected Then
                cadena = cadena + "TientaCaballo." + li.Value + ","
            End If
        Next
        ' Forma la parte de la cadena de la tabla TientaPie
        For Each Item In ListBox3.Items
            Dim li As New ListItem
            li = Item
            If li.Selected Then
                cadena = cadena + "TientaPie." + li.Value + ","
            End If
        Next
        ' Forma la parte de la cadena de la tabla de empadre
        For Each Item In ListBox4.Items
            Dim li As New ListItem
            li = Item
            If li.Selected Then
                cadena = cadena + "empadre." + li.Value + ","
            End If
        Next
        ' Forma la parte de la cadena de la tabla LidiaCaballo
        For Each Item In ListBox5.Items
            Dim li As New ListItem
            li = Item
            If li.Selected Then
                cadena = cadena + "LidiaCaballo." + li.Value + ","
            End If
        Next
        ' Forma la parte de la cadena de la tabla LidiaPie
        For Each Item In ListBox6.Items
            Dim li As New ListItem
            li = Item
            If li.Selected Then
                cadena = cadena + "LidiaPie." + li.Value + ","
            End If
        Next
        ' Forma la parte de la cadena de la tabla de Sanidad
        For Each Item In ListBox7.Items
            Dim li As New ListItem
            li = Item
            If li.Selected Then
                cadena = cadena + "Sanidad." + li.Value + ","
            End If
        Next




        If Len(cadena) = 0 Then
            LblError.Text = "Falta seleccionar almenos un campo"
            Return
        End If
        'forma varieble de memoria, genera reporte de Excel
        Dim LongCampos As Integer = CInt(cadena.Length)
        If LongCampos = 0 Then
            LblError.Text = "Falta seleccionar almenos un campo"
            Return
        Else
        End If
        cadena = cadena.Remove(LongCampos - 1, 1)
    End Sub
    Protected Sub ListBox1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Load
        If Not IsPostBack Then
            LlenaListBox(ListBox1, "animal")
        End If
    End Sub
    Protected Sub ListBox2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.Load
        If Not IsPostBack Then
            LlenaListBox(ListBox2, "TientaCaballo")
        End If
    End Sub
    Protected Sub ListBox3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox3.Load
        If Not IsPostBack Then
            LlenaListBox(ListBox3, "TientaPie")
        End If
    End Sub
    Protected Sub ListBox4_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox4.Load
        If Not IsPostBack Then
            LlenaListBox(ListBox4, "empadre")
        End If
    End Sub
    Protected Sub ListBox5_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox5.Load
        If Not IsPostBack Then
            LlenaListBox(ListBox5, "LidiaCaballo")
        End If
    End Sub
    Protected Sub ListBox6_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox6.Load
        If Not IsPostBack Then
            LlenaListBox(ListBox6, "LidiaPie")
        End If
    End Sub
    Protected Sub ListBox7_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox7.Load
        If Not IsPostBack Then
            LlenaListBox(ListBox7, "Sanidad")
        End If
    End Sub
    Public Sub LlenaListBox(ByRef Control As ListBox, ByVal tabla As String)
        Dim salida As Integer
        Dim CampoReg As String
        Dim Campo As String
        Dim Activo As String
        'Dim Campo As String = "animal"
        Dim ConexionBD As New SqlConnection()
        ConexionBD.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
        Dim OrdenSQL As SqlCommand = Nothing
        Dim Lector As SqlDataReader = Nothing
        Try
            Dim Consulta As String = "Select Campo, Nombre, Tipo, Tabla, Activo, Orden from CamposRep where Tabla= " & "'" & tabla & "'" & " order by Orden"
            OrdenSQL = New SqlCommand(Consulta, ConexionBD)
            ConexionBD.Open()
            Lector = OrdenSQL.ExecuteReader()
            While salida = 0
                If Lector.Read() Then
                    CampoReg = Lector("Nombre")
                    Campo = Lector("Campo")
                    Activo = Lector("Activo")

                    Dim li As New ListItem
                    li.Text = CampoReg
                    li.Value = Campo
                    If Activo = True Then
                        Control.Items.Add(li)
                    End If
                Else
                    salida = 1
                End If
            End While
            ConexionBD.Close()
        Catch ex As Exception
        End Try
        If tabla = "animal" Then
            Dim cadena As String
            For Each Item In ListBox1.Items
                cadena = Item.ToString()
                Select Case cadena
                    Case "sexo"
                        Item.selected = True
                    Case "numero"
                        Item.selected = True
                    Case "letra"
                        Item.selected = True
                    Case "año del animal"
                        Item.selected = True
                    Case "nombre del animal"
                        Item.selected = True
                End Select
            Next
        End If
    End Sub
    Protected Sub BtPantalla_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtPantalla.Click
        LeeCampos()
        Response.Redirect("ReporteGrid.aspx?campos=" + cadena)
    End Sub

    Protected Sub BtExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtExcel.Click
        Dim swhere As String
        LeeCampos()
        campos = Request.QueryString("cadena")
        swhere = util.sWhereFiltro(Session("filtro"))
        Response.Redirect("ReporteExcel.aspx?campos=" + cadena)
    End Sub

    Protected Sub BtnTodos1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnTodos1.Click
        ListBoxActiva(ListBox1)
        LblError.Text = ""
    End Sub

    Protected Sub BtnNinguno1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnNinguno1.Click
        ListBoxDesactiva(ListBox1)
        LblError.Text = ""
    End Sub

    Protected Sub BtnTodos2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnTodos2.Click
        ListBoxActiva(ListBox2)
        LblError.Text = ""
    End Sub

    Protected Sub BtnNinguno2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnNinguno2.Click
        ListBoxDesactiva(ListBox2)
        LblError.Text = ""
    End Sub

    Protected Sub BtnTodos3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnTodos3.Click
        ListBoxActiva(ListBox3)
        LblError.Text = ""
    End Sub

    Protected Sub BtnNinguno3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnNinguno3.Click
        ListBoxDesactiva(ListBox3)
        LblError.Text = ""
    End Sub

    Protected Sub BtnTodos4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnTodos4.Click
        ListBoxActiva(ListBox4)
        LblError.Text = ""
    End Sub
    Protected Sub BtnNinguno4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnNinguno4.Click
        ListBoxDesactiva(ListBox4)
        LblError.Text = ""
    End Sub
    Protected Sub BtnTodos5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnTodos5.Click
        ListBoxActiva(ListBox5)
        LblError.Text = ""
    End Sub
    Protected Sub BtnNinguno5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnNinguno5.Click
        ListBoxDesactiva(ListBox5)
        LblError.Text = ""
    End Sub
    Protected Sub BtnTodos6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnTodos6.Click
        ListBoxActiva(ListBox6)
        LblError.Text = ""
    End Sub
    Protected Sub BtnNinguno6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnNinguno6.Click
        ListBoxDesactiva(ListBox6)
        LblError.Text = ""
    End Sub
    Protected Sub BtnTodos7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnTodos7.Click
        ListBoxActiva(ListBox7)
        LblError.Text = ""
    End Sub
    Protected Sub BtnNinguno7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnNinguno7.Click
        ListBoxDesactiva(ListBox7)
        LblError.Text = ""
    End Sub
End Class
